package com.mmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mmall.common.ServerResponse;
import com.mmall.dao.SkillMapper;
import com.mmall.pojo.Skill;
import com.mmall.pojo.User;
import com.mmall.service.ISkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @CLASS SkillServiceimpl
 * @DESCRIPTION TODO
 * @AUTHor tang
 * @Date 2019-08-22 16:18
 * @VERSION 1.0
 **/
@Service("iSkillService")
public class SkillServiceimpl implements ISkillService {

    @Autowired
    private SkillMapper skillMapper;
    public ServerResponse<String> save(User user,Skill skill){
        if (skill.getId()==null){
            skill.setUserId(user.getId());
            int count = skillMapper.insert(skill);
            if (count>0){
                return ServerResponse.createBySuccessMessage("新增技能成功");
            }
        }else {
            skill.setUserId(user.getId());
            int count = skillMapper.updateUserIdPrimaryKey(skill);
            if (count>0){
                return ServerResponse.createBySuccessMessage("修改技能成功");
            }
        }
        return ServerResponse.createByErrorMessage("新增修改技能失败");
    }

    public ServerResponse<PageInfo> list(Integer userId,String name,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Skill> skillList = skillMapper.selectUserIdName(userId, name);
        PageInfo pageInfo = new PageInfo(skillList);
        pageInfo.setList(skillList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public ServerResponse<String> delete(Integer userId,Integer skillId){
        if (skillId==null||userId==null){
            return ServerResponse.createByErrorMessage("参数不能为空");
        }
        int count = skillMapper.deleteSkillUserId(userId,skillId);
        if (count>0){
            return ServerResponse.createBySuccessMessage("删除技能成功");
        }
        return ServerResponse.createByErrorMessage("删除该技能失败");
    }
}
